<?php
class ControllerHkWechatForeign extends Controller {
private $error = array();

	public function index() {
		$this->language->load('hk/wechat_foreign');

		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('hk/wechat_foreign');


		$this->getList();
	}

	public function add() {
		$this->language->load('hk/wechat_foreign');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/wechat_foreign');
		// var_dump($this->request->post);exit;
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			// var_dump($this->request->post,$this->model_hk_wechat_foreign->getWechatFiles($this->request->post['no_1_child']));exit;
			$this->model_hk_wechat_foreign->addWechatForeign($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function edit() {
		$this->language->load('hk/wechat_foreign');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/wechat_foreign');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			// var_dump($this->request->post);exit;
			$this->model_hk_wechat_foreign->editWechatForeign($this->request->get['wechat_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function repair() {
		$this->language->load('hk/wechat_foreign');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/wechat_foreign');

		if ($this->validateRepair()) {
			$this->model_hk_wechat_foreign->repairCategories();

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getList();
	}

	protected function getList() {
		// var_dump($this->request->request);exit;
		if (isset($this->request->get['filter_customer_name'])) {
			$filter_customer_name = $this->request->get['filter_customer_name'];
		} else {
			$filter_customer_name = null;
		}

		if (isset($this->request->get['filter_admin_name'])) {
			$filter_admin_name = $this->request->get['filter_admin_name'];
		} else {
			$filter_admin_name = null;
		}

		if (isset($this->request->get['filter_admin_tel'])) {
			$filter_admin_tel = $this->request->get['filter_admin_tel'];
		} else {
			$filter_admin_tel = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		if (isset($this->request->get['filter_bank'])) {
			$filter_bank = $this->request->get['filter_bank'];
		} else {
			$filter_bank = null;
		}
		// if (isset($this->request->get['filter_customer_name']) && isset($this->request->get['filter_customer_value']) ) {
		// 	$filter_customer_name = $this->request->get['filter_customer_name'];
		// 	$filter_customer_value = $this->request->get['filter_customer_value'];
		// 	// var_dump(111);exit;
		// } else {
		// 	$filter_customer_name = null;
		// 	$filter_customer_value = null;
		// }


		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';
		if (isset($this->request->get['filter_customer_name'])) {
			$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_admin_name'])) {
			$url .= '&filter_admin_name=' . urlencode(html_entity_decode($this->request->get['filter_admin_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_admin_tel'])) {
			$url .= '&filter_admin_tel=' . urlencode(html_entity_decode($this->request->get['filter_admin_tel'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_bank'])) {
			$url .= '&filter_bank=' . urlencode(html_entity_decode($this->request->get['filter_bank'], ENT_QUOTES, 'UTF-8'));
		}
		// if (isset($this->request->get['filter_customer_name']) && isset($this->request->get['filter_customer_value']) ) {
		// 	$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
		// 	$url .= '&filter_customer_value=' . urlencode(html_entity_decode($this->request->get['filter_customer_value'], ENT_QUOTES, 'UTF-8'));
		// }

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);

		$data['add'] = $this->url->link('hk/wechat_foreign/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$data['delete'] = $this->url->link('hk/wechat_foreign/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$data['repair'] = $this->url->link('hk/wechat_foreign/repair', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$data['wechats'] = array();

		$filter_data = array(
			'filter_customer_name' => $filter_customer_name,
			'filter_admin_tel'     => $filter_admin_tel,
			'filter_admin_name'    => $filter_admin_name,
			'filter_bank'          => $filter_bank,
			'filter_status'        => $filter_status,
			'sort'                 => $sort,
			'order'                => $order,
			'start'                => ($page - 1) * $this->config->get('config_limit_admin'),
			'limit'                => $this->config->get('config_limit_admin')
		);
		$category_total = $this->model_hk_wechat_foreign->getTotalCategories($filter_data);

		$results = $this->model_hk_wechat_foreign->getWechatList($filter_data);
		foreach ($results as $result) {

			$data['wechats'][] = array(
				'wechat_id'                 => $result['wechat_id'],
				'no_1'                      => (!empty($result['no_1']))?'Y':'N',
				'no_1_name'                 => (!empty($result['data']['no_1']))?($result['data']['no_1']['file_name']):'',
				'no_1_url'                  => (!empty($result['data']['no_1']))?substr($result['data']['no_1']['file_url'],54):'', 
				'no_2'                      => (!empty($result['no_2']))?'Y':'N',
				'no_2_name'                 => (!empty($result['data']['no_2']))?($result['data']['no_2']['file_name']):'',
				'no_2_url'                  => (!empty($result['data']['no_2']))?substr($result['data']['no_2']['file_url'],54):'',
				'no_3'                      => (!empty($result['no_3']))?'Y':'N',
				'no_3_name'                 => (!empty($result['data']['no_3']))?($result['data']['no_3']['file_name']):'',
				'no_3_url'                  => (!empty($result['data']['no_3']))?substr($result['data']['no_3']['file_url'],54):'',
				'no_4'                      => (!empty($result['no_4']))?'Y':'N',
				'no_4_name'                 => (!empty($result['data']['no_4']))?($result['data']['no_4']['file_name']):'',
				'no_4_url'                  => (!empty($result['data']['no_4']))?substr($result['data']['no_4']['file_url'],54):'',
				'no_5'                      => (!empty($result['no_5']))?'Y':'N',
				'no_5_name'                 => (!empty($result['data']['no_5']))?($result['data']['no_5']['file_name']):'',
				'no_5_url'                  => (!empty($result['data']['no_5']))?substr($result['data']['no_5']['file_url'],54):'',
				'customer_name'             => $result['customer_name'],
				'admin_name'                => $result['admin_name'],
				'admin_email'               => $result['admin_email'],
				'admin_tel'                 => $result['admin_tel'],
				'company_bank_account_name' => $result['company_bank_account_name'],
				'company_bank_account_num'  => $result['company_bank_account_num'],
				'bank'                      => $result['bank'],
				'wechat_login_name'         => $result['wechat_login_name'],
				'status'         			=> $this->language->get('wechat_status_'.$result['status']),
				'wechat_login_password'     => (isset($result['wechat_login_password']))?$result['wechat_login_password']:'',
				
				'edit'                      => $this->url->link('hk/wechat_foreign/edit', 'token=' . $this->session->data['token'] . '&wechat_id=' . $result['wechat_id'] . $url, 'SSL'),
				'delete'                    => $this->url->link('hk/wechat_foreign/delete', 'token=' . $this->session->data['token'] . '&wechat_id=' . $result['wechat_id'] . $url, 'SSL'),
			);

		}
		// var_dump($data['projects']);exit;
		$data['heading_title']                  = $this->language->get('heading_title');
		
		$data['text_list']                      = $this->language->get('text_list');
		$data['text_num']                      = $this->language->get('text_num');
		$data['text_no_results']                = $this->language->get('text_no_results');
		$data['text_confirm']                   = $this->language->get('text_confirm');
		
		$data['button_filter']                  = $this->language->get('button_filter');
		
		$data['column_customer_search']         = $this->language->get('column_customer_search');
		
		$data['column_project_customer_search'] = $this->language->get('column_project_customer_search');
		$data['column_name']                    = $this->language->get('column_name');
		$data['column_sort_order']              = $this->language->get('column_sort_order');
		$data['column_action']                  = $this->language->get('column_action');
		//wechat_foreign column
		$data['column_status']                      = $this->language->get('column_status');
		$data['column_no_1']                      = $this->language->get('column_no_1');
		$data['column_no_2']                      = $this->language->get('column_no_2');
		$data['column_no_3']                      = $this->language->get('column_no_3');
		$data['column_no_4']                      = $this->language->get('column_no_4');
		$data['column_no_5']                      = $this->language->get('column_no_5');
		$data['column_customer_name']             = $this->language->get('column_customer_name');
		$data['column_admin_name']                = $this->language->get('column_admin_name');
		$data['column_admin_email']               = $this->language->get('column_admin_email');
		$data['column_admin_tel']                 = $this->language->get('column_admin_tel');
		$data['column_company_tel']               = $this->language->get('column_company_tel');
		$data['column_company_bank_account_name'] = $this->language->get('column_company_bank_account_name');
		$data['column_company_bank_account_num']  = $this->language->get('column_company_bank_account_num');
		$data['column_bank']                      = $this->language->get('column_bank');
		$data['column_wechat_login_name']         = $this->language->get('column_wechat_login_name');
		$data['column_wechat_login_password']     = $this->language->get('column_wechat_login_password');

		$data['wechat_status']     = $this->language->get('wechat_status');

		$data['button_add'] = $this->language->get('button_add');
		$data['button_edit'] = $this->language->get('button_edit');
		$data['button_delete'] = $this->language->get('button_delete');
		$data['button_rebuild'] = $this->language->get('button_rebuild');


		$data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}

		if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		$url = '';



		if (isset($this->request->get['filter_customer_name'])) {
			$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_admin_name'])) {
			$url .= '&filter_admin_name=' . urlencode(html_entity_decode($this->request->get['filter_admin_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_admin_tel'])) {
			$url .= '&filter_admin_tel=' . urlencode(html_entity_decode($this->request->get['filter_admin_tel'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_bank'])) {
			$url .= '&filter_bank=' . urlencode(html_entity_decode($this->request->get['filter_bank'], ENT_QUOTES, 'UTF-8'));
		}
		// if (isset($this->request->get['filter_customer_name']) && isset($this->request->get['filter_customer_value']) ) {
		// 	$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
		// 	$url .= '&filter_customer_value=' . urlencode(html_entity_decode($this->request->get['filter_customer_value'], ENT_QUOTES, 'UTF-8'));
		// }

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}


		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['sort_customer_name'] = $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . '&sort=customer_name' . $url, 'SSL');
		$data['sort_admin_name'] = $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . '&sort=admin_name' . $url, 'SSL');
		$data['sort_status'] = $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . '&sort=status' . $url, 'SSL');

		$url = '';

		if (isset($this->request->get['filter_customer_name'])) {
			$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_admin_name'])) {
			$url .= '&filter_admin_name=' . urlencode(html_entity_decode($this->request->get['filter_admin_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_admin_tel'])) {
			$url .= '&filter_admin_tel=' . urlencode(html_entity_decode($this->request->get['filter_admin_tel'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_bank'])) {
			$url .= '&filter_bank=' . urlencode(html_entity_decode($this->request->get['filter_bank'], ENT_QUOTES, 'UTF-8'));
		}
		// if (isset($this->request->get['filter_customer_name']) && isset($this->request->get['filter_customer_value']) ) {
		// 	$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
		// 	$url .= '&filter_customer_value=' . urlencode(html_entity_decode($this->request->get['filter_customer_value'], ENT_QUOTES, 'UTF-8'));
		// }

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}



		$pagination = new Pagination();
		$pagination->total = $category_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_limit_admin');
		$pagination->url = $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

		$data['pagination'] = $pagination->render();

		$data['results'] = sprintf($this->language->get('text_pagination'), ($category_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($category_total - $this->config->get('config_limit_admin'))) ? $category_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $category_total, ceil($category_total / $this->config->get('config_limit_admin')));

		$data['filter_customer_name'] = $filter_customer_name;
		$data['filter_admin_name'] = $filter_admin_name;
		$data['filter_admin_tel'] = $filter_admin_tel;
		$data['filter_bank'] = $filter_bank;
		// $data['filter_customer_value'] = $filter_customer_value;
		$data['filter_status'] = $filter_status;

		$data['sort'] = $sort;
		$data['order'] = $order;

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		// var_dump(111);exit;
		$this->response->setOutput($this->load->view('hk/wechat_foreign_list.tpl', $data));
	}

	protected function getForm() {
		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_form'] = !isset($this->request->get['category_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
		$data['text_none'] = $this->language->get('text_none');
		$data['text_default'] = $this->language->get('text_default');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');

		$data['text_status_1'] = $this->language->get('text_status_1');
		$data['text_status_2'] = $this->language->get('text_status_2');
		$data['text_status_3'] = $this->language->get('text_status_3');
		$data['text_status_4'] = $this->language->get('text_status_4');
		$data['text_status_4'] = $this->language->get('text_status_4');
		$data['text_status_0'] = $this->language->get('text_status_0');

		$data['text_confirm_yes'] = $this->language->get('text_confirm_yes');
		$data['text_confirm_no'] = $this->language->get('text_confirm_no');

		$data['entry_upload'] = $this->language->get('entry_upload');
		$data['entry_overwrite'] = $this->language->get('entry_overwrite');
		$data['entry_progress'] = $this->language->get('entry_progress');

		$data['help_upload'] = $this->language->get('help_upload');

		$data['button_upload'] = $this->language->get('button_upload');
		$data['button_clear'] = $this->language->get('button_clear');
		$data['button_continue'] = $this->language->get('button_continue');

		//wechat_foreign entry
		$data['entry_status']                    = $this->language->get('entry_status');
		$data['entry_no_1']                      = $this->language->get('entry_no_1');
		$data['entry_no_2']                      = $this->language->get('entry_no_2');
		$data['entry_no_3']                      = $this->language->get('entry_no_3');
		$data['entry_no_4']                      = $this->language->get('entry_no_4');
		$data['entry_no_5']                      = $this->language->get('entry_no_5');
		$data['entry_customer_name']             = $this->language->get('entry_customer_name');
		$data['entry_admin_name']                = $this->language->get('entry_admin_name');
		$data['entry_admin_email']               = $this->language->get('entry_admin_email');
		$data['entry_admin_tel']                 = $this->language->get('entry_admin_tel');
		$data['entry_company_tel']               = $this->language->get('entry_company_tel');
		$data['entry_company_bank_account_name'] = $this->language->get('entry_company_bank_account_name');
		$data['entry_company_bank_account_num']  = $this->language->get('entry_company_bank_account_num');
		$data['entry_bank']                      = $this->language->get('entry_bank');
		$data['entry_wechat_login_name']         = $this->language->get('entry_wechat_login_name');
		$data['entry_wechat_login_password']     = $this->language->get('entry_wechat_login_password');


		$data['wechat_status']     = $this->language->get('wechat_status');

		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');

		$data['tab_general'] = $this->language->get('tab_general');
		$data['tab_data'] = $this->language->get('tab_data');
		$data['tab_design'] = $this->language->get('tab_design');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['customer_name'])) {
			$data['error_customer_name'] = $this->error['customer_name'];
		} else {
			$data['error_customer_name'] = null;
		}



		$url = '';
		if (isset($this->request->get['filter_ckg_number'])) {
			$url .= '&filter_ckg_number=' . urlencode(html_entity_decode($this->request->get['filter_ckg_number'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_project_name'])) {
			$url .= '&filter_project_name=' . urlencode(html_entity_decode($this->request->get['filter_project_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_english_name_of_supplier'])) {
			$url .= '&filter_english_name_of_supplier=' . urlencode(html_entity_decode($this->request->get['filter_english_name_of_supplier'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_customer_name']) && isset($this->request->get['filter_customer_value']) ) {
			$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
			$url .= '&filter_customer_value=' . urlencode(html_entity_decode($this->request->get['filter_customer_value'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);

		if (!isset($this->request->get['wechat_id'])) {
			$data['action'] = $this->url->link('hk/wechat_foreign/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
			$data['wechat_id'] = '';
		} else {
			$data['action'] = $this->url->link('hk/wechat_foreign/edit', 'token=' . $this->session->data['token'] . '&wechat_id=' . $this->request->get['wechat_id'] . $url, 'SSL');
			$data['wechat_id'] = $this->request->get['wechat_id'];
		}

		$data['cancel'] = $this->url->link('hk/wechat_foreign', 'token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['wechat_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$wecaht_info = $this->model_hk_wechat_foreign->getWecahtForeign($this->request->get['wechat_id']);
		}

		$data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');

		$data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['customer_name'])) {
			$data['customer_name'] = $this->request->post['customer_name'];
		} elseif (!empty($wecaht_info)) {
			$data['customer_name'] = $wecaht_info['customer_name'];
		} else {
			$data['customer_name'] = '';
		}

		if (isset($this->request->post['admin_name'])) {
			$data['admin_name'] = $this->request->post['admin_name'];
		} elseif (!empty($wecaht_info)) {
			$data['admin_name'] = $wecaht_info['admin_name'];
		} else {
			$data['admin_name'] = '';
		}

		if (isset($this->request->post['admin_email'])) {
			$data['admin_email'] = $this->request->post['admin_email'];
		} elseif (!empty($wecaht_info)) {
			$data['admin_email'] = $wecaht_info['admin_email'];
		} else {
			$data['admin_email'] = '';
		}

		if (isset($this->request->post['admin_tel'])) {
			$data['admin_tel'] = $this->request->post['admin_tel'];
		} elseif (!empty($wecaht_info)) {
			$data['admin_tel'] = $wecaht_info['admin_tel'];
		} else {
			$data['admin_tel'] = '';
		}


		if (isset($this->request->post['company_bank_account_name'])) {
			$data['company_bank_account_name'] = $this->request->post['company_bank_account_name'];
		} elseif (!empty($wecaht_info)) {
			$data['company_bank_account_name'] = $wecaht_info['company_bank_account_name'];
		} else {
			$data['company_bank_account_name'] = '';
		}

		if (isset($this->request->post['company_bank_account_num'])) {
			$data['company_bank_account_num'] = $this->request->post['company_bank_account_num'];
		} elseif (!empty($wecaht_info)) {
			$data['company_bank_account_num'] = $wecaht_info['company_bank_account_num'];
		} else {
			$data['company_bank_account_num'] = '';
		}

		if (isset($this->request->post['bank'])) {
			$data['bank'] = $this->request->post['bank'];
		} elseif (!empty($wecaht_info)) {
			$data['bank'] = $wecaht_info['bank'];
		} else {
			$data['bank'] = '';
		}

		if (isset($this->request->post['wechat_login_name'])) {
			$data['wechat_login_name'] = $this->request->post['wechat_login_name'];
		} elseif (!empty($wecaht_info)) {
			$data['wechat_login_name'] = $wecaht_info['wechat_login_name'];
		} else {
			$data['wechat_login_name'] = '';
		}

		if (isset($this->request->post['wechat_login_password'])) {
			$data['wechat_login_password'] = $this->request->post['wechat_login_password'];
		} elseif (!empty($wecaht_info)) {
			$data['wechat_login_password'] = $wecaht_info['wechat_login_password'];
		} else {
			$data['wechat_login_password'] = '';
		}

		if(isset($this->request->post['no_1_child'])){
			// var_dump($this->request->post);exit;
			$data['no_1_childs'] = $this->model_hk_wechat_foreign->getWechatFiles($this->request->post['no_1_child']);
		} elseif (!empty($wecaht_info)) {
			if(!empty($wecaht_info['data']['no_1'])){
				$data['no_1_childs'] = $wecaht_info['data']['no_1'];
			}else{
				$data['no_1_childs'] = null;
			}
		}else{
			$data['no_1_childs'] = null;
		}

		if(isset($this->request->post['no_2_child'])){
			// var_dump($this->request->post);exit;
			$data['no_2_childs'] = $this->model_hk_wechat_foreign->getWechatFiles($this->request->post['no_2_child']);
		} elseif (!empty($wecaht_info)) {
			if(!empty($wecaht_info['data']['no_2'])){
				$data['no_2_childs'] = $wecaht_info['data']['no_2'];
			}else{
				$data['no_2_childs'] = null;
			}
		}else{
			$data['no_2_childs'] = null;
		}

		if(isset($this->request->post['no_3_child'])){
			// var_dump($this->request->post);exit;
			$data['no_3_childs'] = $this->model_hk_wechat_foreign->getWechatFiles($this->request->post['no_3_child']);
		} elseif (!empty($wecaht_info)) {
			if(!empty($wecaht_info['data']['no_3'])){
				$data['no_3_childs'] = $wecaht_info['data']['no_3'];
			}else{
				$data['no_3_childs'] = null;
			}
		}else{
			$data['no_3_childs'] = null;
		}

		if(isset($this->request->post['no_4_child'])){
			// var_dump($this->request->post);exit;
			$data['no_4_childs'] = $this->model_hk_wechat_foreign->getWechatFiles($this->request->post['no_4_child']);
		} elseif (!empty($wecaht_info)) {
			if(!empty($wecaht_info['data']['no_4'])){
				$data['no_4_childs'] = $wecaht_info['data']['no_4'];
			}else{
				$data['no_4_childs'] = null;
			}
		}else{
			$data['no_4_childs'] = null;
		}

		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (!empty($wecaht_info)) {
			$data['status'] = $wecaht_info['status'];
		} else {
			$data['status'] = '';
		}


		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('hk/wechat_foreign_form.tpl', $data));
	}

	protected function validateForm() {

		if ((utf8_strlen(trim($this->request->post['customer_name'])) < 1) || (utf8_strlen(trim($this->request->post['customer_name'])) > 200)) {
			$this->error['customer_name'] = $this->language->get('error_project_name');
		}

		
		return !$this->error;
	}


	protected function validateRepair() {
		if (!$this->user->hasPermission('modify', 'hk/wechat_foreign')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}
	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_ckg_number']) || isset($this->request->get['filter_project_name']) ) {
			$this->load->model('hk/wechat_foreign');
			if (isset($this->request->get['filter_ckg_number'])) {
				$filter_ckg_number = $this->request->get['filter_ckg_number'];
			} else {
				$filter_ckg_number = '';
			}

			if (isset($this->request->get['filter_project_name'])) {
				$filter_project_name = $this->request->get['filter_project_name'];
			} else {
				$filter_project_name = '';
			}

			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];
			} else {
				$limit = 5;
			}

			$filter_data = array(
				'filter_ckg_number'  => $filter_ckg_number,
				'filter_project_name' => $filter_project_name,
				'start'        => 0,
				'limit'        => $limit
			);

			$results = $this->model_hk_wechat_foreign->getVendorChecklists($filter_data);
			// var_dump($results);exit;
			foreach ($results as $result) {
				$json[] = array(
					'project_progress_id' => $result['project_progress_id'],
					'ckg_number'        => strip_tags(html_entity_decode($result['ckg_number'], ENT_QUOTES, 'UTF-8')),
					'project_name'        => strip_tags(html_entity_decode($result['project_name'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}



		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}


	public function clear() {
		$this->load->language('hk/wechat_foreign');
		$this->load->model('hk/wechat_foreign');

		$json = array();

		if (!$this->user->hasPermission('modify', 'hk/wechat_foreign')) {
			$json['error'] = $this->language->get('error_permission');
		}

		if (!$json) {

			$file = $this->request->request['name'];
			
			if(!empty($file)){
				$wechat_id = isset($this->request->request['wechat_id'])?$this->request->request['wechat_id']:null;
				$query	= $this->model_hk_wechat_foreign->clearFile($wechat_id,$this->request->request['id'],$this->request->request['name']);

				
				
				if (is_file($query)) {
					unlink($query);
				} elseif (is_dir($query)) {
					rmdir($query);
				}

				$json['success'] = $this->language->get('text_clear');
			}else{
				$json['msg'] = 'No File!';
			}
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function file_upload(){
		$data['token'] = $this->session->data['token'];
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$this->response->setOutput($this->load->view('hk/wechat_upload.tpl', $data));
	}
	public function upload() {
		$this->language->load('hk/wechat_foreign');
		$this->load->model('hk/wechat_foreign');

		$json = array();

		// Check user has permission
		if (!$this->user->hasPermission('modify', 'hk/wechat_foreign')) {
			$json['error'] = $this->language->get('error_permission');
		}

		// Make sure we have the correct directory
		if (isset($this->request->get['directory'])) {
			$directory = rtrim(DIR_IMAGE.'wechat' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
		} else {
			$directory = DIR_IMAGE.'wechat';
		}

		// Check its a directory
		if (!is_dir($directory)) {
			$json['error'] = $this->language->get('error_directory');
		}

		if (!$json) {
			if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
				// Sanitize the filename
				$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));

				// Validate the filename length
				if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
					$json['error'] = $this->language->get('error_filename');
				}

				// Allowed file extension types
				$allowed = array();
				$FileTypeExt = "png
jpe
jpeg
jpg
pdf
xls
xlsx
docx
doc";
				$FileTypeMime = "image/png
image/jpeg
application/msword
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/pdf";
				$extension_allowed = preg_replace('~\r?\n~', "\n", $FileTypeExt);

				$filetypes = explode("\n", $extension_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}
				// var_dump($this->config->get('config_file_mime_allowed'));exit;
				// Allowed file mime types
				$allowed = array();

				$mime_allowed = preg_replace('~\r?\n~', "\n", $FileTypeMime);

				$filetypes = explode("\n", $mime_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array($this->request->files['file']['type'], $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Check to see if any PHP files are trying to be uploaded
				$content = file_get_contents($this->request->files['file']['tmp_name']);

				if (preg_match('/\<\?php/i', $content)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Return any upload error
				if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
					$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
				}
			} else {
				$json['error'] = $this->language->get('error_upload');
			}
		}

		if (!$json) {
			$file = token(16).'_'.$this->request->files['file']['name'];
			$uploadfile = $directory . '/' .$file;
			move_uploaded_file($this->request->files['file']['tmp_name'], $uploadfile);
			$wechat_file_id = $this->model_hk_wechat_foreign->addFile($filename,$uploadfile);
			$json['filename'] = $file;
			$json['mask'] = $filename;
			$json['uploadfile'] = $uploadfile;
			$json['wechat_file_id'] = $wechat_file_id;
			$json['tmp_name'] = $this->request->files['file']['tmp_name'];
			$json['code'] = 0;
			$json['success'] = $this->language->get('text_upload');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function getFiles(){
		$this->load->model('hk/wechat_foreign');
		// $file_ids = ($this->request->post['da'])?$this->request->post['da']：'';
		// $file_id = implode(",",$file_ids);
		if(!empty($this->request->post['da'])){
			$file_id = implode(",",$this->request->post['da']);
			$result = $this->model_hk_wechat_foreign->getFiles($file_id);
			if($result){
				$json = array(
					'status' => 1,
					'msg' => 'success',
					'project_file' => $result,
				);
			}else{
				$json = array(
					'status' =>0,
					'msg' => 'error',
				);
			}
		}else{
			$json = array(
					'status' => 2,
					'msg' => 'error',
				);
		}
		$this->response->addHeader('Content-Type: application/json');
		echo json_encode($json);
	}
}